<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\ding\ShopApply as ShopApplyModel;
use app\api\model\Page as PageModel;
use app\api\model\store\Shop as ShopModel;
use app\common\service\Day as DayService;
use think\facade\Db;

/**
 * 页面控制器
 * Class Index
 * @package app\api\controller
 */
class Page extends Controller
{
    /**
     * 页面数据
     * @param int|null $pageId 页面ID, 不传的话默认为首页
     * @return array|\think\response\Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $pageId = null)
    {
        Db::name('ding_order')->where('source', 0)->update(['order_status' => 10]);
        // 页面数据
        $model    = new PageModel;
        $pageData = $model->getPageData($pageId);
        return $this->renderSuccess(compact('pageData'));
    }

    public function applyAdd()
    {
        $model = new ShopApplyModel;

        if ($model->add($this->postForm())) {
            return $this->renderSuccess([], '提交成功，工作人员会与您联系');
        }
        return $this->renderError($model->getError() ?: '提交失败');

    }
    public function getPeople($start = 1, $end = 100)
    {
        $list = [];
        for ($i = $start; $i <= $end; $i++) {

            $list[] = ['label' => $i, 'value' => $i];
        }
        return $this->renderSuccess(compact('list'));
    }
    /*
		*用户预订页面，可预订时间，1周
	*/

    public function getYuyueDate($endTime)
    {
        $end_date = Date('Y-m-d ') . $endTime;
        $end_date = strtotime($end_date);

        $time = strtotime(Date('Y-m-d'));

        $date = [];

        for ($i = 0; $i < 30; $i++) {
            $week = get_week_day($time);
            if ($i == 0 && $end_date < time()) {
                $time += 86400;
                continue;
            }
            if ($i == 0) {
                $arr['label'] = '今天 ' . $week;
            } elseif ($i == 1) {
                $arr['label'] = '明天 ' . $week;
            } elseif ($i == 2) {
                $arr['label'] = '后天 ' . $week;
            } else {
                $arr['label'] = date('m-d', $time) . ' ' . $week;
            }
            $arr['value'] = $i;
            $arr['date']  = date('Y-m-d', $time);
            $arr['time']  = date('m-d', $time);
            $arr['week']  = $week;

            $time += 86400;
            $date[] = $arr;
        }

        return $this->renderSuccess(compact('date'));
    }

    public function getDate($type = 1, $time = '')
    {

        if ($time == '') {
            $date['date'] = date('Y-m-d');
        } else {
            $date['date'] = $time;
            if ($type == 2) {
                $date['date'] = Date('Y-m-d', strtotime($time) + 86400);
            } elseif ($type == 1) {
                $date['date'] = Date('Y-m-d', strtotime($time) - 86400);

            }

        }
        $date['is_past'] = 0;
        if (strtotime($date['date']) < strtotime(Date('Y-m-d'))) {
            $date['is_past'] = 1;

        }
        $c = new DayService();

        $date_arr = explode('-', $date['date']);

        $date['week'] = $c->get_week_day($date['date']);

        $today = $c->convertSolarToLunar($date_arr[0], $date_arr[1], $date_arr[2]);
        $lunar = $today[1] . $today[2];

        $date['lunar'] = $lunar;

        return $this->renderSuccess(compact('date'));
    }
    public function getTime($type = 1)
    {
        $start_date = Date('2024-12-12 10:00');
        $start_date = strtotime($start_date);
        $time1      = [];
        $time       = [];
        for ($i = 1; $i < 11; $i++) {
            $time['label'] = Date('H:i', $start_date);
            $time['value'] = $i;
            $start_date += 30 * 60;
            $time1[] = $time;

        }
        $time1[] = ['label' => '自定义', 'value' => 11];
        $time1[] = ['label' => '待定', 'value' => 12];

        $start_date = Date('2024-12-12 15:00');
        $start_date = strtotime($start_date);
        $time2      = [];
        $dd         = [];
        for ($i = 1; $i < 11; $i++) {
            $dd['label'] = Date('H:i', $start_date);
            $dd['value'] = $i;
            $start_date += 30 * 60;
            $time2[] = $dd;

        }
        $time2[] = ['label' => '自定义', 'value' => 11];
        $time2[] = ['label' => '待定', 'value' => 12];

        $start_date = Date('2024-12-12 00:00');
        $start_date = strtotime($start_date);

        $time = [];
        for ($i = 1; $i < 49; $i++) {
            $time22['label'] = Date('H:i', $start_date);
            $time22['value'] = $i;
            $start_date += 30 * 60;

            $time[] = $time22;

        }

        if ($type == 1) {
            $time = $time1;
        } elseif ($type == 2) {
            $time = $time2;
        } else {
            $time = $time;
        }
        $children = [];
        for ($i = 0; $i < 60; $i++) {
            $dd = $i;
            if ($i < 10) {
                $dd = '0' . $i;
            }
            $time22['label'] = $dd;
            $time22['value'] = $i;
            $children[]      = $time22;
        }

        $list = [];
        for ($i = 0; $i < 24; $i++) {
            $dd = $i;
            if ($i < 10) {
                $dd = '0' . $i;
            }
            $time22['label']    = $dd;
            $time22['value']    = $i;
            $time22['children'] = $children;
            $list[]             = $time22;

        }

        return $this->renderSuccess(compact('time', 'time1', 'time2', 'list'));
    }
    public function getTime2()
    {

        $start_date = Date('2024-12-12 00:00');
        $start_date = strtotime($start_date);
        $time1      = [];
        $time11     = [];
        for ($i = 1; $i < 31; $i++) {
            $time11['label'] = Date('H:i', $start_date);
            $time11['value'] = $i;
            $start_date += 30 * 60;
            $time1[] = $time11;

        }

        $start_date = Date('2024-12-12 15:00');
        $start_date = strtotime($start_date);
        $time2      = [];
        $dd         = [];
        for ($i = 1; $i < 19; $i++) {
            $dd['label'] = Date('H:i', $start_date);
            $dd['value'] = $i;
            $start_date += 30 * 60;
            $time2[] = $dd;

        }

        $time[] = ['value' => 1, 'label' => '午餐', 'children' => $time1];
        $time[] = ['value' => 2, 'label' => '晚餐', 'children' => $time2];

        return $this->renderSuccess(compact('time'));
    }
    /*
		*获取指定城市区域
	*/
    public function getDistrict($city)
    {
        $city_id = Db::name('region')->where('name', $city)->value('id');

        $list = Db::name('region')->where('pid', $city_id)->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }
    /*
		*邀请函样式
	*/
    public function getType()
    {

        $type = Db::name('ding_letter')->where('is_delete', 0)->order('sort asc')->select()->toArray();

        return $this->renderSuccess(compact('type'));
    }

    /*
		*菜品
	*/
    public function getFoodsList()
    {

        $list = Db::name('ding_shop_foods')->where('is_delete', 0)->order('sort asc')->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }
    /*
		*餐厅类型
	*/
    public function getShopTypeList()
    {

        $list = Db::name('ding_shop_type')->where('is_delete', 0)->order('sort asc')->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }
    /*
		*人均消费
	*/
    public function getMoneyList()
    {

        $list = [['label' => '30~50', 'min' => 30, 'max' => 50], ['label' => '50~100', 'min' => 50, 'max' => 100], ['label' => '100~150', 'min' => 100, 'max' => 150], ['label' => '150~200', 'min' => 150, 'max' => 200], ['label' => '200~250', 'min' => 200, 'max' => 250], ['label' => '250~300', 'min' => 250, 'max' => 300], ['label' => '300~350', 'min' => 300, 'max' => 350], ['label' => '350~400', 'min' => 350, 'max' => 400], ['label' => '400以上', 'min' => 400, 'max' => 10000]];

        return $this->renderSuccess(compact('list'));
    }

    /*
		*员工角色
	*/
    public function getRoleList()
    {

        $list = Db::name('store_shop_clerk_role')->where('is_delete', 0)->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }

    /*
		*预约基本数据
	*/
    public function getYuyueData()
    {

        $peopleList = [];
        for ($i = 2; $i < 25; $i++) {
            $peopleList[] = $i;
        }
        $timeList  = [];
        $strtotime = strtotime('2024-03-23 10:00');
        for ($i = 0; $i < 25; $i++) {
            $timeList[] = Date('H:i', $strtotime);
            $strtotime += 30 * 60;
        }

        $dateList = [];

        $time = time();

        for ($i = 0; $i < 7; $i++) {
            if ($i == 0) {
                $date['name'] = '今天';
                $date['date'] = Date('m.d', $time);
                $date['year'] = Date('Y-m-d', $time);
            }
            if ($i == 1) {
                $date['name'] = '明天';
                $date['date'] = Date('m.d', $time);
                $date['year'] = Date('Y-m-d', $time);
            }
            if ($i > 1) {
                $date['name'] = get_week_day($time);
                $date['date'] = Date('m.d', $time);
                $date['year'] = Date('Y-m-d', $time);

            }
            $time += 86400;

            $dateList[] = $date;
        }

        return $this->renderSuccess(compact('peopleList', 'timeList', 'dateList'));
    }

    /*
		*生日日期
	*/
    public function getDateList()
    {

        for ($i = 1; $i < 13; $i++) {
            $month[] = ['label' => $i . '月', 'value' => $i];

        }
        for ($i = 1; $i < 31; $i++) {
            $day[] = ['label' => $i . '日', 'value' => $i];

        }
        $list1[] = $month;
        $list1[] = $day;

        $month = [['label' => '正月', 'value' => 1], ['label' => '二月', 'value' => 2], ['label' => '三月', 'value' => 3], ['label' => '四月', 'value' => 4], ['label' => '五月', 'value' => 5], ['label' => '六月', 'value' => 6], ['label' => '七月', 'value' => 7], ['label' => '八月', 'value' => 8], ['label' => '九月', 'value' => 9], ['label' => '十月', 'value' => 10], ['label' => '十一月', 'value' => 11],
            ['label' => '腊月', 'value' => 12]];

        $day = [['label' => '初一', 'value' => 1], ['label' => '初二', 'value' => 2], ['label' => '初三', 'value' => 3], ['label' => '初四', 'value' => 4], ['label' => '初五', 'value' => 5], ['label' => '初六', 'value' => 6], ['label' => '初七', 'value' => 7], ['label' => '初八', 'value' => 8], ['label' => '初九', 'value' => 9], ['label' => '初十', 'value' => 10], ['label' => '十一', 'value' => 11], ['label' => '十二', 'value' => 12], ['label' => '十三', 'value' => 13], ['label' => '十四', 'value' => 14], ['label' => '十五', 'value' => 15], ['label' => '十六', 'value' => 16], ['label' => '十七', 'value' => 17], ['label' => '十八', 'value' => 18], ['label' => '十九', 'value' => 19], ['label' => '二十', 'value' => 20], ['label' => '廿一', 'value' => 21], ['label' => '廿二', 'value' => 22], ['label' => '廿三', 'value' => 23], ['label' => '廿四', 'value' => 24], ['label' => '廿五', 'value' => 25], ['label' => '廿六', 'value' => 26], ['label' => '廿七', 'value' => 27], ['label' => '廿八', 'value' => 28], ['label' => '廿九', 'value' => 29], ['label' => '三十', 'value' => 30]];

        $list2[] = $month;
        $list2[] = $day;

        return $this->renderSuccess(compact('list1', 'list2'));
    }

    /*
		*已经开通城市
	*/
    public function getOpenList($search = '')
    {
        $filter = [];
        if (! empty($search)) {
            $filter[] = ['shop.city', 'like', '%' . $search . '%'];
        }
        $list = Db::name('store_shop')
            ->alias('shop')
            ->join('region', 'region.id=shop.city_id')
            ->where($filter)
            ->group('shop.city_id')
            ->field('shop.city_id,region.name as city')->select()->toArray();
        return $this->renderSuccess(compact('list'));
    }

    /*
		*根据关键字搜索周边地区
	*/
    public function mapList($search = '', $city, $longitude, $latitude)
    {
        $model = new ShopModel;

        $list = $model->getMapList($search, $city, $longitude, $latitude);

        return $this->renderSuccess(compact('list'));
    }

}
